function [] = part1()
    clc
    clear

    X = [0 1 1 0 0 0 1 1];
    Y = [1 1 1 1 1 0 1 0];
    fprintf('x: %s\n',mat2str(X));
    fprintf('y: %s\n\n',mat2str(Y));
    
    len = length(X);
    
    % Initial stage
    disp('init');
    G = zeros(1,len);
    P = zeros(1,len);
    for i = len:-1:1,
        [G(i),P(i)] = GP(X(i),Y(i),i);
    end
    
    fprintf('g: %s\n',mat2str(G));
    fprintf('p: %s\n\n',mat2str(P));

    % 1st stage
    disp('stage1');
    G1 = G;
    P1 = P;
    g0 = 0;
    g1 = 0;
    p0 = 0;
    p1 = 0;
    for i = len:-1:1,
        if mod(i,2)
            g1 = G(i);
            p1 = P(i);
            [G1(i),P1(i)] = dot(g0,g1,p0,p1,i);
        else
            g0 = G(i);
            p0 = P(i);
        end
    end

    fprintf('g: %s\n',mat2str(G1));
    fprintf('p: %s\n\n',mat2str(P1));

    % 2nd stage
    disp('stage2');
    G2 = G1;
    P2 = P1;
    g1 = 0;
    g2 = 0;
    p1 = 0;
    p2 = 0;
    for i = len:-1:1,
        if(i == 7 || i == 3)
            g1 = G1(i);
            p1 = P1(i);
        elseif(i == 6 || i == 5 || i == 2 || i == 1)
            g2 = G1(i);
            p2 = P1(i);
            [G2(i),P2(i)] = dot(g1,g2,p1,p2,i);            
        end
    end

    fprintf('g: %s\n',mat2str(G2));
    fprintf('p: %s\n\n',mat2str(P2));
    
    % 3rd stage
    disp('stage3');
    G3 = G2;
    P3 = P2;
    g4 = 0;
    g5 = 0;
    p4 = 0;
    p5 = 0;
    for i = len:-1:1,
        if(i == 5)
            g4 = G2(i);
            p4 = P2(i);
        elseif(i <= 4)
            g5 = G2(i);
            p5 = P2(i);
            [G3(i),P3(i)] = dot(g4,g5,p4,p5,i);
        end
    end
    
    fprintf('g: %s\n',mat2str(G3));
    fprintf('p: %s\n\n',mat2str(P3));
    
    % carry
    disp('carry');
    C = zeros(1,len);
    c_in = 0;
    for i = len:-1:1,
        C(i) = or(G3(i),and(P3(i),c_in));
        t = 8-i;
        fprintf('c%d=g%d+p%d*c%d=%d+%d*%d=%d\n',t,t,t,t-1,G3(i),P3(i),c_in,C(i));
        c_in = C(i);
    end
    %C = [C(2:end) 0];

    fprintf('c: %s\n\n',mat2str(C));
    
    % sum
    disp('sum');
    S = zeros(1,len);
    S2 = zeros(1,len);
    c_in = 0;
    for i = len:-1:1,
        S(i) = xor(xor(X(i),Y(i)),c_in);
        t = 8-i;
        c_in = C(i);
        fprintf('s%d=x%d xor y%d xor c%d=%d xor %d xr %d=%d\n',t,t,t,t-1,X(i),Y(i),c_in,S(i));
    end
    
    fprintf('c_out=%d\n',C(1));
    
    S = [C(1) S];
    
    fprintf('s : %s\n',mat2str(S));

    s = din2dec(S);
    fprintf('s : %d\n',s);
    
    disp('facit');
    ss = dec2bin(bin2dec('01100011')+bin2dec('11111010'));
    fprintf('s : %d\n',bin2dec(ss));
    
end

function [g,p] = GP(ai,bi,i)
    i = 8-i;
    g = and(ai,bi);
    p = or(ai,bi);
    fprintf('g = x%d*y%d = %d*%d = %d\n',i,i,ai,bi,g);
    fprintf('p = x%d+y%d = %d+%d = %d\n',i,i,ai,bi,p);
end

function [g,p] = dot(gi_prev,gi,pi_prev,pi,i)
    i = 8-i;
    g = or(gi,and(pi,gi_prev));
    p = and(pi,pi_prev);
    fprintf('g = g%d+p%d*g%d = %d+%d*%d = %d\n',i,i,i-1,gi,pi,gi_prev,g);
    fprintf('p = p%d*p%d = %d*%d = %d\n',i,i-1,pi,pi_prev,p);
end

function d = din2dec(B)
    len = length(B);
    d = sum(2.^(len-1:-1:0).*B);
end